package com.mycity.gamification;


/**
 * The LevelCalculator class calculates users' current levels and 
 * updates their experiences after a contribution.
 * It stores constant data for which contribution gives how much experience.
 * @author  Ahmet Erdem
 * @version December 2013 
 */
public class LevelCalculator {
	
	private static final int LEVEL_UP_EXP = 5;
	
	private static final int EXP_COEFF = 2;
	
	private static final int POST_EXP = 4;	
	private static final int STAR_EXP = 3;	
	private static final int DIAMOND_EXP = 2;	
	private static final int COMMENT_EXP = 1;
	
	public enum Objects {
		POST, STAR, DIAMOND, COMMENT
	}

	/**
	 * Finds current level for given experience.
	 * Formula: EXPERIENCE = LEVEL_UP_EXP * (EXP_COEFF ^ LEVEL)
	 * @param experience a user's current experience
	 * @return level of the user
	 */
	public static int findLevel(int experience){
		int level = 0;
		
		experience/=LEVEL_UP_EXP;
		
		if(experience==0)
			return level;
		
		while(experience>0){
			level++;
			experience/=EXP_COEFF;
		}
		
		return level;
	}
	
	/**
	 * Updates experience based on the type of object.
	 * If sign=1 it is a gain, else if sign=-1 it is a lose.
	 * @param object it may be post, comment, diamond or star.
	 * @param currentExp experience before update
	 * @param sign gain or lose (1 or -1)
	 * @return updated experience
	 */
	public static int updateExp(Objects object, int currentExp, int sign){
		int exp=currentExp;
		
		switch(object){
		case COMMENT:
			exp+=COMMENT_EXP*sign;
			break;
		case DIAMOND:
			exp+=DIAMOND_EXP*sign;
			break;
		case POST:
			exp+=POST_EXP*sign;
			break;
		case STAR:
			exp+=STAR_EXP*sign;
			break;
		default:
			break;
		}
		return exp;
		
	}

}
